<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="指标名称" prop="zbName">
        <el-input
          v-model="queryParams.zbName"
          placeholder="请输入指标名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指标简称" prop="zbJname">
        <el-input
          v-model="queryParams.zbJname"
          placeholder="请输入指标简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--
      <el-form-item label="指标层级" prop="zbLevel">
        <el-select v-model="queryParams.zbLevel" placeholder="请选择指标层级" clearable>
          <el-option
            v-for="dict in dict.type.sys_zb_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:zbmanage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="zbmanageList"
      row-key="zbId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <!--<el-table-column label="父部门id" prop="parentId" />
      <el-table-column label="祖级列表" align="center" prop="ancestors" />-->
      <el-table-column label="指标名称" align="left" prop="zbName"  width="320" :show-overflow-tooltip="true"/>
      <el-table-column label="指标简称" align="left" prop="zbJname" width="280" :show-overflow-tooltip="true"/>
      <!--
      <el-table-column label="指标层级" align="center" prop="zbLevel" width="90">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_zb_level" :value="scope.row.zbLevel"/>
        </template>
      </el-table-column>-->
      <el-table-column label="指标分数" align="center" prop="zbFz" width="90"/>
      <el-table-column label="显示顺序" align="center" prop="orderNum" width="80"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="说明" align="center" prop="introduction" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status" width="90">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:zbmanage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-show="scope.row.zbLevel != '3'"
            v-hasPermi="['business:zbmanage:add']"
          >新增下级</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCopy(scope.row)"
            v-show="scope.row.parentId === 0"
            v-hasPermi="['business:zbmanage:edit']"
          >复制</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:zbmanage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改指标管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="720px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="父部门id" prop="parentId">
          <treeselect v-model="form.parentId" :options="zbmanageOptions" :normalizer="normalizer" placeholder="请选择父部门id" />
        </el-form-item>
        <el-form-item label="指标名称" prop="zbName">
          <el-input v-model="form.zbName" placeholder="请输入指标名称" />
        </el-form-item>
        <el-form-item label="指标简称" prop="zbJname">
          <el-input v-model="form.zbJname" placeholder="请输入指标简称" />
        </el-form-item>
        <!--
        <el-form-item label="指标层级" prop="zbLevel">
          <el-radio-group v-model="form.zbLevel">
            <el-radio
              v-for="dict in dict.type.sys_zb_level"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-row>
          <el-col :span="12" v-if="form.zbLevel != '3'">
            <el-form-item label="一票否决指标" prop="isVeto" >
              <el-radio-group v-model="form.isVeto">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no_num"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.zbLevel == '1'">
            <el-form-item label="增补指标" prop="isBonusZb">
              <el-radio-group v-model="form.isBonusZb">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no_num"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否打分指标" prop="isDfZb">
              <el-radio-group v-model="form.isDfZb">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no_num"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="指标分数" prop="zbFz">
              <el-input-number v-model="form.zbFz" placeholder="请输入指标分数" :step="0.01" :max="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="orderNum">
              <el-input-number v-model="form.orderNum" placeholder="请输入显示顺序" :max="9999"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" rows="5" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="说明" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listZbmanage, getZbmanage, delZbmanage, addZbmanage, updateZbmanage,copyZbmanage } from "@/api/business/zbmanage";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Zbmanage",
  dicts: ['sys_status', 'sys_zb_level','sys_yes_no_num'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 指标管理表格数据
      zbmanageList: [],
      // 指标管理树选项
      zbmanageOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        zbName: null,
        zbJname: null,
        zbLevel: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "父部门id不能为空", trigger: "blur" }
        ],
        zbName: [
          { required: true, message: "指标名称不能为空", trigger: "blur" }
        ],
        zbJname: [
          { required: true, message: "指标简称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],

        /**
        zbLevel: [
          { required: true, message: "指标层级不能为空", trigger: "change" }
        ],**/
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询指标管理列表 */
    getList() {
      this.loading = true;
      listZbmanage(this.queryParams).then(response => {
        this.zbmanageList = this.handleTree(response.data, "zbId", "parentId");
        this.loading = false;
      });
    },
    /** 转换指标管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.zbId,
        label: node.zbName,
        children: node.children
      };
    },
	/** 查询指标管理下拉树结构 */
    getTreeselect() {
      listZbmanage().then(response => {
        this.zbmanageOptions = [];
        const data = { zbId: 0, zbName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "zbId", "parentId");
        this.zbmanageOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        zbId: null,
        parentId: null,
        ancestors: null,
        zbName: null,
        zbJname: null,
        zbLevel: null,
        zbFz: null,
        isDfZb:'1',
        orderNum: null,
        remark: null,
        introduction: null,
        isVeto: '1',
        isBonusZb: '1',
        status: '0',
        delFlag: '0',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delTime: null,
        delBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      if(row.zbLevel === '3'){
        this.$alert("一共就三级指标！");
        return ;
      }
      this.reset();
      this.getTreeselect();
      if (row != null && row.zbId) {
        this.form.parentId = row.zbId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加指标管理";
      if(row.zbLevel === '1'){
        this.form.zbLevel = '2';
      }else if(row.zbLevel === '2'){
        this.form.zbLevel = '3';
      }
      if(this.form.parentId===0){
        this.form.zbLevel = '1';
      }
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getZbmanage(row.zbId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改指标管理";
      });
    },
    /** 复制按钮操作 */
    handleCopy(row) {
      this.$modal.confirm('是否要进行【复制】一级指标，指标名称为【' + row.zbName + '】的数据项吗？').then(function() {
        return copyZbmanage(row.zbId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("复制成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.zbId != null) {
            updateZbmanage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZbmanage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除指标管理编号为"' + row.zbId + '"的数据项？').then(function() {
        return delZbmanage(row.zbId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
